package www.com.library.view;

import android.content.Context;
import android.graphics.Typeface;
import android.text.SpannableString;
import android.text.SpannableStringBuilder;
import android.text.style.AbsoluteSizeSpan;
import android.text.style.StyleSpan;
import android.text.style.SuperscriptSpan;

/**
 * 价格处理类，控制价格小数位数字体变大
 * @author Administrator
 *
 */
public class PriceSpannable {
	private static class PriceSpanConfig {
		public final static int IGNORED = 0xFFFF;

		public final int spanEnlargeFirstChar;
		public final int spanEnlargeSecondChar;
		public final int spanSuperscriptChar;

		public PriceSpanConfig(int enlargeFirst, int enlargeSecond, int superscript) {
			spanEnlargeFirstChar = enlargeFirst;
			spanEnlargeSecondChar = enlargeSecond;
			spanSuperscriptChar = superscript;
		}
	}

	private static final PriceSpanConfig[] mPriceSpanConfigs = { new PriceSpanConfig(PriceSpanConfig.IGNORED, PriceSpanConfig.IGNORED, PriceSpanConfig.IGNORED), // 0 decimal point
			new PriceSpanConfig(-1, PriceSpanConfig.IGNORED, PriceSpanConfig.IGNORED), // 1 decimal point
			new PriceSpanConfig(-2, -1, PriceSpanConfig.IGNORED), // 2 decimalpoints
			new PriceSpanConfig(-3, -2, -1), // 3 decimal points
			new PriceSpanConfig(-2, -1, PriceSpanConfig.IGNORED), // 4 decimal points
			new PriceSpanConfig(-3, -2, -1), // 5 decimal points
			new PriceSpanConfig(-3, -2, -1), // 6 decimal points
			new PriceSpanConfig(-3, -2, -1), // 7 decimal points
			new PriceSpanConfig(-3, -2, -1), // 8 decimal points
	};

	private Context mCtx;
	private AbsoluteSizeSpan mFirstCharSizeSpan;
	private AbsoluteSizeSpan mSecondCharSizeSpan;
	private AbsoluteSizeSpan mThirdCharSizeSpan;
	private SuperscriptSpan mSuperSpan;
	private SpannableStringBuilder mSpanBuilder;

	public PriceSpannable(Context ctx) {
		mCtx = ctx;
		mFirstCharSizeSpan = new AbsoluteSizeSpan(22, true);
		mSecondCharSizeSpan = new AbsoluteSizeSpan(22, true);
		mThirdCharSizeSpan = new AbsoluteSizeSpan(22, true);
		mSuperSpan = new SuperscriptSpan();
		mSpanBuilder = new SpannableStringBuilder();
		mSpanBuilder.clear();
	}

	/**
	 * 根据需要此处设置后缀文字的大小
	 * @param ctx
	 * @param superSize 后缀文字的大小,R文件中的id值
	 */
	public PriceSpannable(Context ctx, int superSize) {
		mCtx = ctx;
		mFirstCharSizeSpan = new AbsoluteSizeSpan(mCtx.getResources().getDimensionPixelSize(superSize), true);
		mSecondCharSizeSpan = new AbsoluteSizeSpan(mCtx.getResources().getDimensionPixelSize(superSize), true);
		mThirdCharSizeSpan = new AbsoluteSizeSpan(mCtx.getResources().getDimensionPixelSize(superSize), true);
		mSuperSpan = new SuperscriptSpan();
		mSpanBuilder = new SpannableStringBuilder();
		mSpanBuilder.clear();
	}

	public SpannableStringBuilder createSpannableString(int digits, int point, String price) {
		PriceSpanConfig config;

		if (digits < 0 || digits >= mPriceSpanConfigs.length)
			config = mPriceSpanConfigs[0];
		else
			config = mPriceSpanConfigs[digits];

		mSpanBuilder.clear();
		mSpanBuilder.append(price);

		final int priceLen = price.length() - point;
		final int shift1Char = priceLen + config.spanEnlargeFirstChar;
		final int shift2Char = priceLen + config.spanEnlargeSecondChar;
		final int shiftSuperscriptChar = priceLen + config.spanSuperscriptChar;

		if (shift1Char < 0)
			return mSpanBuilder;
		if (shift2Char < 0)
			return mSpanBuilder;
		if (shiftSuperscriptChar < 0)
			return mSpanBuilder;
		//这是包括小数点一共有三位的情况，小数点在中间则放大小数点前后两位
		if(digits>2){
			if (config.spanEnlargeFirstChar != PriceSpanConfig.IGNORED) {
				mSpanBuilder.setSpan(mFirstCharSizeSpan, shift1Char, shift1Char + 1, SpannableString.SPAN_EXCLUSIVE_EXCLUSIVE);
//				mSpanBuilder.setSpan(new StyleSpan(Typeface.BOLD), shift1Char, shift1Char + 1, SpannableString.SPAN_EXCLUSIVE_EXCLUSIVE);
			}

			if (config.spanEnlargeSecondChar != PriceSpanConfig.IGNORED) {
				mSpanBuilder.setSpan(mSecondCharSizeSpan, shift2Char, shift2Char + 1, SpannableString.SPAN_EXCLUSIVE_EXCLUSIVE);
//				mSpanBuilder.setSpan(new StyleSpan(Typeface.BOLD), shift2Char, shift2Char + 1, SpannableString.SPAN_EXCLUSIVE_EXCLUSIVE);
			}

			if (config.spanSuperscriptChar != PriceSpanConfig.IGNORED) {
				mSpanBuilder.setSpan(mThirdCharSizeSpan, shiftSuperscriptChar, shiftSuperscriptChar + 1, SpannableString.SPAN_EXCLUSIVE_EXCLUSIVE);
//				mSpanBuilder.setSpan(new StyleSpan(Typeface.BOLD), shiftSuperscriptChar, shiftSuperscriptChar + 1, SpannableString.SPAN_EXCLUSIVE_EXCLUSIVE);
			}
		}else{
			if (config.spanEnlargeFirstChar != PriceSpanConfig.IGNORED) {
				mSpanBuilder.setSpan(mFirstCharSizeSpan, shift1Char, shift1Char + 1, SpannableString.SPAN_EXCLUSIVE_EXCLUSIVE);
//				mSpanBuilder.setSpan(new StyleSpan(Typeface.BOLD), shift1Char, shift1Char + 1, SpannableString.SPAN_EXCLUSIVE_EXCLUSIVE);
			}

			if (config.spanEnlargeSecondChar != PriceSpanConfig.IGNORED) {
				mSpanBuilder.setSpan(mSecondCharSizeSpan, shift2Char, shift2Char + 1, SpannableString.SPAN_EXCLUSIVE_EXCLUSIVE);
//				mSpanBuilder.setSpan(new StyleSpan(Typeface.BOLD), shift2Char, shift2Char + 1, SpannableString.SPAN_EXCLUSIVE_EXCLUSIVE);
			}
		}

		while(point > 0){
			mSpanBuilder.setSpan(mSuperSpan, price.length()-point, price.length()-point+1, SpannableString.SPAN_EXCLUSIVE_EXCLUSIVE);
			point = point -1;
		}


		return mSpanBuilder;
	}

	/**
	 * 报价放大缩小控件处理
	 * @param startIndex
	 * @param nums
	 * @param price
     * @return
     */
	public SpannableStringBuilder createSpannableStrings(int startIndex, int nums, String price) {

		mSpanBuilder.clear();
		mSpanBuilder.append(price);

		final int priceLen = price.length();
		int superStart = startIndex + nums;
		final int shiftSuperscriptChar = priceLen - superStart;

		if(superStart <= priceLen){
			mSpanBuilder.setSpan(mFirstCharSizeSpan, startIndex, startIndex + nums, SpannableString.SPAN_EXCLUSIVE_EXCLUSIVE);
//			mSpanBuilder.setSpan(new StyleSpan(Typeface.BOLD), startIndex, startIndex + nums, SpannableString.SPAN_EXCLUSIVE_EXCLUSIVE);
		}
		if (shiftSuperscriptChar > 0){
			if(price.charAt(superStart) == '.'){
				superStart++;
			}
			mSpanBuilder.setSpan(mSuperSpan, superStart, priceLen, SpannableString.SPAN_EXCLUSIVE_EXCLUSIVE);
		}

		return mSpanBuilder;
	}
}
